﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SkladLib
{
   public class Rezervace
    {
       public string Name;
       public delegate void RezervaceRemoveEventHandler(object sender, int id);
       public event RezervaceRemoveEventHandler Removing;


        public static Rezervace DictionaryToRezervace(Dictionary<string, string> dictionary)
        {
            Rezervace rezervace= new Rezervace
                {
                    Name = dictionary["jmeno"],
                    DateFrom = DateTime.Parse(dictionary["od"]),
                    DateIn = DateTime.Parse(dictionary["do"]),
                    _idPolozka = dictionary["idPolozka"],
                    IdRezervace = int.Parse(dictionary["idRezervace"])
                };
            return rezervace;
        }


        public void RemoveFromDatabase()
        {
            DataLib.Settings.databases.dbSklad.Query("DELETE FROM Rezervace WHERE [idRezervace] = " + IdRezervace);
            if (Removing != null)
                Removing(this, IdRezervace);
        }

        public static Rezervace GetRezervaceById(int idRezervace)
        {
            Dictionary<string,string> row = DataLib.Settings.databases.dbSklad.QueryGetRow("SELECT * FROM Rezervace WHERE [idRezervace] = " + idRezervace);
            return DictionaryToRezervace(row);
        }

        public Dictionary<string,string> ToDictionary()
        {
            Dictionary<string, string> result = new Dictionary<string, string>
                {
                    {"idRezervace", IdRezervace.ToString()},
                    {"idPolozka", _idPolozka.ToString()},
                    {"od", DateFrom.ToString()},
                    {"do", DateIn.ToString()},
                    {"jmeno", Name}
                };
            return result;
        }

        public void SaveToDB()
        {
            DataLib.Settings.databases.dbSklad.AddRow("Rezervace", ToDictionary());
        }


        public DateTime DateFrom;


        public DateTime DateIn;

       
        private string _idPolozka;


        public int IdRezervace;

    }
}
